package com.bocai.dahuatech.ui.frag;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bocai.dahuatech.Bean.HomeBannerBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseFragment;
import com.bocai.dahuatech.ui.activity.NewsDetailActivity;
import com.bocai.dahuatech.ui.activity.NewProductDetailActivity;
import com.bocai.dahuatech.ui.activity.PromotionProductActivity;
import com.bumptech.glide.Glide;

import butterknife.ButterKnife;


public class BannerFrag extends BaseFragment {
    private View view;
    private ImageView iv_banner;
    private HomeBannerBean.ContentEntity data;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.banner_grag, container, false);
        initView();
        return view;
    }
    public void getData(HomeBannerBean.ContentEntity data){
        this.data=data;


    }

    private void initData() {
            Glide.with(getActivity()).load(data.getImageUrl()).into(iv_banner);


    }

    private void initView() {
        DisplayMetrics dm=new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width=dm.widthPixels;
        int height=width/2;
        ButterKnife.bind(this, view);
        iv_banner=(ImageView)view.findViewById(R.id.iv_banner);
        ViewGroup.LayoutParams params=iv_banner.getLayoutParams();
        params.width=width;
        params.height=height;
        iv_banner.setLayoutParams(params);
        initData();
        iv_banner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if("0".equals(data.getType())){//跳转到产品详情
                    if(!data.getProductId().equals("")){
                        Intent intent=new Intent(getActivity(), NewProductDetailActivity.class);
                        intent.putExtra("productId",Integer.parseInt(data.getProductId()));
                        intent.putExtra("solutionId","");
                        startActivity(intent);
                    }
                }else if("1".equals(data.getType())){//跳转到促销产品列表
//                    if(SharePrefencesUtil.getifpromotion(getActivity()).equals("true")){
                        Intent intent=new Intent(getActivity(), PromotionProductActivity.class);
                        intent.putExtra("what","banner");
                        startActivity(intent);
//                    }
                }else if("2".equals(data.getType())){//
                    Intent intent=new Intent(getActivity(), NewsDetailActivity.class);
                    intent.putExtra("newsurl",data.getProductId());
                    intent.putExtra("what",0);
                    startActivity(intent);
                }
            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
